package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ThreeMeanwhile;
import com.ruoyi.system.domain.ViewConfigPushMes;
import com.ruoyi.system.entity.SysRemind;
import com.ruoyi.system.mapper.ThreeMeanwhileMapper;
import com.ruoyi.system.mapper.ViewConfigPushMesMapper;
import com.ruoyi.system.service.SysRemindService;
import com.ruoyi.system.service.ThreeMeanwhileService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 三同时手续(ThreeMeanwhile)表服务实现类
 *
 * @author makejava
 * @since 2022-12-15 08:49:46
 */
@Service("threeMeanwhileService")
public class ThreeMeanwhileServiceImpl implements ThreeMeanwhileService {
    @Resource
    private ThreeMeanwhileMapper threeMeanwhileDao;

    @Autowired
    private SysRemindService sysRemindService;
    @Resource
    private ViewConfigPushMesMapper viewConfigPushMesMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ThreeMeanwhile queryById(Integer id) {
        return this.threeMeanwhileDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param threeMeanwhile 筛选条件
     * @param pageRequest    分页对象
     * @return 查询结果
     */
    @Override
    public Page<ThreeMeanwhile> queryByPage(ThreeMeanwhile threeMeanwhile, PageRequest pageRequest) {
        long total = this.threeMeanwhileDao.count(threeMeanwhile);
        return new PageImpl<>(this.threeMeanwhileDao.queryAllByLimit(threeMeanwhile, pageRequest), pageRequest, total);
    }

    @Override
    public List<ThreeMeanwhile> queryAll(ThreeMeanwhile threeMeanwhile) {
        List<ThreeMeanwhile> threeMeanwhiles = threeMeanwhileDao.queryAll(threeMeanwhile);
        if (!CollectionUtils.isEmpty(threeMeanwhiles)) {
            for (ThreeMeanwhile var : threeMeanwhiles) {
                if (null == var.getRemindDate()) {
                    var.setColor(0);
                    continue;
                }
                //获取当前时间
                String toDateStr = DateUtils.getDate();
                Date toDate = new Date();

                //获取提醒日期
                String remind = var.getRemindDate();
                Date date = DateUtils.parseDate(remind);

                if (toDateStr.equals(remind)) {
                    var.setColor(1);
                    continue;
                }

                boolean after = toDate.after(date);
                if (after) {
                    var.setColor(2);
                    continue;
                }

                var.setColor(0);
            }
        }
        return threeMeanwhiles;
    }

    /**
     * 新增数据
     *
     * @param threeMeanwhile 实例对象
     * @return 实例对象
     */
    @Override
    public ThreeMeanwhile insert(ThreeMeanwhile threeMeanwhile) {
        threeMeanwhile.setCreatedDate(DateUtils.getNowDate());
        this.threeMeanwhileDao.insert(threeMeanwhile);
        return threeMeanwhile;
    }

    /**
     * 修改数据
     *
     * @param threeMeanwhile 实例对象
     * @return 实例对象
     */
    @Override
    public ThreeMeanwhile update(ThreeMeanwhile threeMeanwhile) {
        this.threeMeanwhileDao.update(threeMeanwhile);
        return this.queryById(threeMeanwhile.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.threeMeanwhileDao.deleteById(id) > 0;
    }
    //@Scheduled(cron = "0 0 6 * * ?")
    @Scheduled(cron = "0 0 6 * * ?")
    public void checkPermitReportFile() {
        ThreeMeanwhile threeMeanwhile = new ThreeMeanwhile();
        List<ThreeMeanwhile> relateds = threeMeanwhileDao.queryAll(threeMeanwhile);
        if (org.springframework.util.CollectionUtils.isEmpty(relateds)) {
            return;
        }

        relateds.forEach(var -> {
            String remind = var.getRemindDate();
            Date date = new Date();
            boolean after = date.after(DateUtils.parseDate(remind));
            if (after) {
                String stringBuffer =
                        "名称:" + var.getName() +
                                "即将到期。";

                ViewConfigPushMes viewConfigPushMes = new ViewConfigPushMes();
                viewConfigPushMes.setViewName("三同时手续");
                List<ViewConfigPushMes> configPushMes = viewConfigPushMesMapper.queryByViewKey(viewConfigPushMes);
                if (!org.springframework.util.CollectionUtils.isEmpty(configPushMes)) {
                    ViewConfigPushMes pushMes = configPushMes.stream().findFirst().orElse(new ViewConfigPushMes());
                    if (com.ruoyi.common.utils.StringUtils.isNotEmpty(pushMes.getViewValue())) {
                        boolean contains = pushMes.getViewValue().contains(",");
                        if (contains) {
                            String[] split = pushMes.getViewValue().split(",");
                            for (String vars : split) {
                                sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(vars));
                            }
                        } else {
                            sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(pushMes.getViewValue()));
                        }
                    }
                }
            }
        });
    }


}
